package ar.com.globallogic.promomr.domain;

import java.util.ArrayList;
import java.util.List;

import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;

import ar.com.globallogic.promomr.exceptions.DuplicatedNodeNameException;

@Document(collection="folders")
public class Folder extends Node {
	
	@DBRef
	private List<Node> children;
	
	public Folder() {
		super();
		this.children = new ArrayList<Node>();
	}

	public Folder(String name){
		super(name);
		this.children = new ArrayList<Node>();
	}
		
	public void addChild(Node node){
		testNameForChild(node);
		node.setParentId(this.getId());
		children.add(node);
	}
	
	public void testNameForChild(Node node){
		for(Node child : children){
			if(areNameAndTypeEquals(node, child)){
				throw new DuplicatedNodeNameException(node.getName(), this.getName());
			}
		}		
	}

	private boolean areNameAndTypeEquals(Node node, Node child) {
		return child.getName().equals(node.getName()) && child.getClass() == node.getClass();
	}
	
	public int getAmountOfChildren(){
		return children.size();
	}
	
	public List<Node> getChildren() {
		return children;
	}

	public void setChildren(List<Node> children) {
		this.children = children;
	}

}
